package tech.runchen.gz5055.oms.modules.system.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import tech.runchen.gz5055.oms.common.annotation.RequiresPermissions;
import tech.runchen.gz5055.oms.common.annotation.SysLog;
import tech.runchen.gz5055.oms.common.base.AbstractController;
import tech.runchen.gz5055.oms.common.enums.BusinessType;
import tech.runchen.gz5055.oms.common.enums.UserType;
import tech.runchen.gz5055.oms.common.utils.PageInfo;
import tech.runchen.gz5055.oms.common.utils.Result;
import tech.runchen.gz5055.oms.common.utils.poi.ExcelImportDataListener;
import tech.runchen.gz5055.oms.common.utils.poi.ExcelUtil;
import tech.runchen.gz5055.oms.common.utils.poi.head.ImportProductExcel;
import tech.runchen.gz5055.oms.modules.system.entity.ProductEntity;
import tech.runchen.gz5055.oms.modules.system.service.IProductService;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 商品信息表 前端控制器
 * </p>
 *
 * @author zzj
 * @since 2023-10-10
 */
@Api(tags = "管理平台 - 商品管理")
@RestController
@RequestMapping("/sys/product")
public class SysProductController extends AbstractController {

    @Autowired
    private IProductService productService;

    @ApiOperation("商品列表（分页")
    @RequiresPermissions(value = {UserType.SPD, UserType.NURSE})
    @GetMapping("/list")
    public Result list(@RequestParam Map<String, Object> params) {
        PageInfo page = productService.queryPage(params);
        return Result.success(page);
    }

    @ApiOperation("商品列表（不分页")
    @RequiresPermissions(value = {UserType.SPD, UserType.NURSE})
    @GetMapping("/select")
    public Result select(@RequestParam Map<String, Object> params) {
        List<ProductEntity> list = productService.querySelect(params);
        return Result.success(list);
    }

    @ApiOperation("商品详情")
    @RequiresPermissions(value = {UserType.SPD, UserType.NURSE})
    @GetMapping("/{productId}")
    public Result info(@PathVariable("productId") Long productId) {
        ProductEntity product = productService.getById(productId);
        return Result.success(product);
    }

    @SysLog(title = "保存商品", businessType = BusinessType.INSERT)
    @ApiOperation("保存商品信息")
    @RequiresPermissions(value = {UserType.SPD})
    @PostMapping("/save")
    public Result save(@Validated @RequestBody ProductEntity product) {
        productService.saveProduct(product);
        return Result.success();
    }

    @SysLog(title = "更新商品", businessType = BusinessType.UPDATE)
    @ApiOperation("更新商品信息")
    @RequiresPermissions(value = {UserType.SPD})
    @PostMapping("/update")
    public Result update(@Validated @RequestBody ProductEntity product) {
        productService.updateProduct(product);
        return Result.success();
    }

    @SysLog(title = "删除商品", businessType = BusinessType.DELETE)
    @ApiOperation("删除商品信息")
    @RequiresPermissions()
    @DeleteMapping("/delete/{productId}")
    public Result delete(@PathVariable("productId") long productId) {
        productService.deleteProduct(productId);
        return Result.success();
    }

    @SysLog(title = "导入商品信息", businessType = BusinessType.IMPORT)
    @ApiOperation("导入商品信息")
    @RequiresPermissions()
    @RequestMapping("/import")
    public Result importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        ExcelUtil util = new ExcelUtil(ImportProductExcel.class);
        util.importExcel(file.getInputStream(), new ExcelImportDataListener<ImportProductExcel>(productService));
        return Result.success("商品导入成功");
    }
}
